package com.mecuros.handler.pending;

import cn.hutool.core.collection.CollUtil;
import com.mecuros.common.domain.TaskInfo;
import com.mecuros.handler.deduplication.DeduplicationRuleService;
import com.mecuros.handler.deduplication.DiscardMessageService;
import com.mecuros.handler.handler.HandlerHolder;
import lombok.Data;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 * @Classname Task
 * @Description
 * @Date 2022/5/15 6:47 下午
 * @Created by yujianbo
 */
@Data
@Accessors(chain = true)
@Slf4j
@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class Task implements Runnable {

    @Autowired
    private HandlerHolder handlerHolder;

    @Autowired
    private DeduplicationRuleService deduplicationRuleService;


    @Autowired
    private TaskInfo taskInfo;

    @Autowired
    private DiscardMessageService discardMessageService;

    @Override
    public void run() {

        // 0. 丢弃消息
        if (discardMessageService.isDiscard(taskInfo)) {
            return;
        }

        // 1. TODO 通用去重
        deduplicationRuleService.duplication(taskInfo);


        // 2. 真正发送消息
        if (CollUtil.isNotEmpty(taskInfo.getReceiver())) {
            handlerHolder.route(taskInfo.getSendChannel())
                    .doHandler(taskInfo);
        }

    }
}
